package com.tbit.uqbike.client.remote;

import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.util.DateTimeUtil;
import com.tbit.uqbike.client.util.HttpUrlUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: chen
 * @Description: 支付分模板消息推送
 * @Date: 创建于 10:26 2020/10/14
 */
@Slf4j
public class WxzffModelMsgUtil {


    /**
     * 发送消息模板
     *
     * @return
     */
    public static void sendTemplate(String weixinAccessToken, String toUser, String template_id, String startTime, String endTime) {
        JSONObject parameters = new JSONObject();
        parameters.put("access_token", weixinAccessToken);
        parameters.put("touser", toUser);
        parameters.put("template_id", template_id);
        JSONObject time = new JSONObject();
        JSONObject number01 = new JSONObject();
        Date start = DateTimeUtil.StringToDateTime(startTime);
        Date end = DateTimeUtil.StringToDateTime(endTime);
        long tmpo = (((end.getTime() - start.getTime()) / 1000) / 60);
        if ((((end.getTime() - start.getTime()) / 1000) % 60) > 0) {
            tmpo = tmpo + 1;
        }
        number01.put("value", "骑行" + tmpo + "分钟");
        JSONObject number02 = new JSONObject();
        number02.put("value", startTime);
        JSONObject number03 = new JSONObject();
        number03.put("value", endTime);
        time.put("thing1", number01);
        time.put("time2", number02);
        time.put("time3", number03);
        parameters.put("data", time);
        String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + weixinAccessToken;
        log.info("发送模板消息地址： " + url);
        log.info("发送模板消息参数： " + parameters.toString());
        JSONObject jsonObjectTemplate;
        try {
            String data = HttpRequest.post(url)
                    .header(Header.CONTENT_TYPE, "application/json")
                    .header(Header.ACCEPT, "application/json")
                    //请求主体
                    .body(JSONObject.toJSONString(parameters))
                    .execute().body();
            //结果转成Object对象
            jsonObjectTemplate = JSONObject.parseObject(data);
            log.info("发送消息模板返回参数" + jsonObjectTemplate);
        } catch (Exception e) {
            throw new BaseException("500", "连接超时");
        }
    }

    /**
     * 获取access_token
     *
     * @return
     */
    public synchronized static String getAccessToken(Integer accountId, String appid, String secret) {
        String weixinAccessToken = "";
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("url", "https://api.weixin.qq.com/cgi-bin/token");
        map.put("grant_type", "client_credential");
        map.put("appid", appid);
        map.put("secret", secret);
        String urlJoint = HttpUrlUtil.urlJoint(map);
        log.info("获取accessToken请求地址：" + urlJoint);
        JSONObject jsonObject = null;
        try {
            String date = HttpRequest.get(urlJoint)
                    .header(Header.CONTENT_TYPE, "application/json")
                    .header(Header.ACCEPT, "application/json")
                    .body("")
                    .execute().body();
            jsonObject = JSONObject.parseObject(date);
            weixinAccessToken = jsonObject.getString("access_token");
            log.info("获取accessToken返回数据：   " + jsonObject);
        } catch (Exception e) {
            throw new BaseException("500", "网络超时！");
        }
        return weixinAccessToken;
    }


}
